jQuery(document).ready(function($){
	var contextPath = $.getContextPath();
	var baseUri = '/siteimages/';
	var loading = contextPath + '/resources/images/image-loading.gif';
	var noImage = contextPath + '/resources/images/no_images.jpg';
	var liTpl = '<li title="{4}"><a href="javascript:void(0)" {3}><img src="{0}" alt="{1}" data="{2}" height="80px" width="80px"/></a><span>#{4}</span></li>';
	var imgInfo = {
		page: 1,
		rows: 20,
		total: 0,
		length: 0
	};
	var imgData = [];
	var current = 0;
	var $imgPage = $('#imgPage');
	$('#bigImgSrc').attr('src', loading);
	
	$.setDateController('hour');
	
	//自动播放函数
	var interval;
	var autoPlayFunc = function() {
		if (current + 1 >= imgInfo.length) {
			if (imgInfo.page+1>imgInfo.total) {
				alert('已经是最后一页了');
				stopPlay();
			} else {
				imgInfo.page++;
				getImgs();
			}
		} else {
			$('#bigImgSrc').attr('src', baseUri + imgData[++current].reference.bigImg.src);
			$('#imgBar li a').removeClass('selected');
			$('#imgBar li a:eq('+current+')').addClass('selected');
		};
	};
	var startPlay = function() {
		interval = setInterval(autoPlayFunc, 2000);
		$('#autoPlayBtn').val("停止播放");
		$('#autoPlayBtn').one('click', stopPlay);
	};
	var stopPlay = function() {
		clearInterval(interval);
		$('#autoPlayBtn').val("自动播放");
		$('#autoPlayBtn').one('click', startPlay);
	};
	
	$('#autoPlayBtn').button({ disabled: true });
	
	var queryFunc = function(){
		var $form = $('#queryform');
		$.ajax({
			url: $form.attr('action'),
			type: $form.attr('method'),
			data: $form.serialize() + "&" + $.param(imgInfo),
			dataType: 'json',
			error: function(){
				$('#bigImgSrc').attr('src', noImage);
			},
			success:function(data) {
				var imgBar = $('#imgBar'), rows = data.rows;
				if (data.records == 0) $('#bigImgSrc').attr('src', noImage);
				imgData = data.rows;
				imgInfo.total = data.total;
				imgInfo.length = (imgData || 0 ) && imgData.length;
				imgBar.empty();
				for (var i=0, max=rows.length; i<max; i++) {
					imgBar.append($.format(liTpl, baseUri + rows[i].src, rows[i].alt, i, i==0?"class='selected'":"", rows[i].title||''));
				}
				current = 0;
				$('#bigImgSrc').attr('src', baseUri + imgData[current].reference.bigImg.src);
				$imgPage.text(data.page+"/"+data.total+"页");
				$('#autoPlayBtn').button({ disabled: false });
			}
		});
	};
	
	$('#queryBtn').button().click(function(){
		stopPlay();
		$('#autoPlayBtn').button({ disabled: true });
		queryFunc();
	});
	
	$('#bimgleft, #bimgright').css({opacity: 0});
	$('#bigImg').hover(function(){
		$('#bimgleft, #bimgright').css({opacity: 1});
	}, function(){
		$('#bimgleft, #bimgright').css({opacity: 0});
	});
	
	var getImgs = function(){
		queryFunc();
	};
	getImgs();
	
	$('#imgBar').click(function(e){
		var target = e.target ? e.target : e.srcElement, 
			$target = $(target),
			src = $target.attr('data');
		if (target.tagName.toUpperCase() != "IMG") return;
		$('#imgBar li a').removeClass('selected');
		src = imgData[src].reference.bigImg.src;
		$('#bigImgSrc').attr('src', baseUri + src);
		$target.parent().addClass("selected");
		current = parseInt($target.attr('data'), 10);
	});
	
	var nextPage = function() {
		if (imgInfo.page+1>imgInfo.total) {
			alert('已经是最后一页了');
		} else {
			imgInfo.page++;
			getImgs();
		}
	};
	
	var prePage = function() {
		if (imgInfo.page == 1) {
			alert("已经是第一页了");
		} else {
			imgInfo.page--;
			getImgs();
		}
	};
	
	$('#bimgright').click(function(e){
		if (current + 1 >= imgInfo.length) {
			nextPage();
		} else {
			$('#bigImgSrc').attr('src', baseUri + imgData[++current].reference.bigImg.src);
			$('#imgBar li a').removeClass('selected');
			$('#imgBar li a:eq('+current+')').addClass('selected');
		};
	});
	
	$('#bimgleft').click(function(e){
		if (current - 1 < 0) {
			prePage();
		} else {
			$('#bigImgSrc').attr('src', baseUri + imgData[--current].reference.bigImg.src);
			$('#imgBar li a').removeClass('selected');
			$('#imgBar li a:eq('+current+')').addClass('selected');
		}
	});
	
	$('#prePage').click(function(){
		prePage();
	});
	
	$('#nextPage').click(function(){
		nextPage();
	});
});